
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import  {_API} from '@/api/bai'

const types = {

  SET_KUFANGLIST: 'SET_KUFANGLIST',//库房字典
  SET_DATA_DRUGLIST:'SET_DATA_DRUGLIST',//药品分类
  SET_DATA_SPECIES:'SET_DATA_SPECIES',//药品种类
  SET_DATA_HOUSE:'SET_DATA_HOUSE',//药房字典

  SET_DATA_BINGQU:'SET_DATA_BINGQU',//药房字典——病区
  SET_DATA_FORM_OF_DRUG:'SET_DATA_FORM_OF_DRUG',

  SET_DATA_DICT_LIST:'SET_DATA_DICT_LIST',//字典
  SET_DATA_DICT_psn_cert_type:'SET_DATA_DICT_psn_cert_type', //字典
  SET_DATA_DICT_naty:'SET_DATA_DICT_naty',

  SET_DATA_SUBJECT:'SET_DATA_SUBJECT',
 
}
let distPatam={
  64:'dict_psn_cert_type',
  37:'dict_naty',
  13:'yibao_insutype',  //险种类型
  14:'yibao_psn_type',
  20:'yibao_med_type',  //医疗类型
  15:'dict_med_type',  //医疗类别
  67:'dict_diag_type',  //诊断类别
  2:'dict_positional', //职称
  71:'dict_member_type',
  37:'dict_nation'

}
const state = {
        KuFangList:[],  //字典客房
        drugList:[],  //药品分类
        speciesList:[],//药品种类
        bingQuList:[],
        houseList:[],
        form_of_drugList:[],//剂形
        dict_psn_cert_type:[], //字典
        dict_naty:[],
        yibao_insutype:[],
        yibao_psn_type:[],
        yibao_med_type:[],
        dict_med_type:[],
        dict_diag_type:[],
        dict_positional:[],
        dict_member_type:[],
        vuex_subject:[],
        dict_nation:[],

}

const mutations = {
  [types.SET_DATA_SUBJECT]:(state, name)=>{
    
    state.vuex_subject=name;
  },
  [types.SET_DATA_DICT_LIST]:(state, name)=>{
   
    name.data.map(v => {
     
           state[distPatam[v.id]]=v.items; 
    });
   
  },
 
  [types.SET_KUFANGLIST]:(state, name)=> {
    state.KuFangList = name
  },
  [types.SET_DATA_DRUGLIST]:(state, name)=> {
    state.drugList = name
  },
  [types.SET_DATA_SPECIES]:(state, name)=> {
    state.speciesList = name
  },
  [types.SET_DATA_HOUSE]:(state, name)=> {
    state.houseList = name
  },
  [types.SET_DATA_BINGQU]:(state, name)=> {
    state.bingQuList = name
  },
  [types.SET_DATA_FORM_OF_DRUG]:(state, name)=> {
    state.form_of_drugList = name
  },
}

const actions = {
  getDeptIndex({ commit }, d){
    return new Promise((resolve, reject) => {
      _API.getDeptIndex(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
                   
                 
                     
                      commit('SET_DATA_SUBJECT', {data:res.data})
                   
                 
             
            
              resolve(res)
              console.log("res",res.data)
          } else {
         
              commit('SET_DATA_DICT_LIST', {})
         
        
             
              resolve()
          }
      })
    })
  },
  getDict_dataDictIndex({ commit }, d){
    return new Promise((resolve, reject) => {
      _API.getDict_dataDictIndex(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
                   
                 
                     
                      commit('SET_DATA_DICT_LIST', {data:res.data})
                   
                 
             
            
              resolve(res)
              console.log("res",res.data)
          } else {
         
              commit('SET_DATA_DICT_LIST', {})
         
        
             
              resolve()
          }
      })
    })
  },

  getdosageGetList({ commit }, d){
    return new Promise((resolve, reject) => {
      _API.getdosageGetList(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
              commit('SET_DATA_FORM_OF_DRUG', res.data.list.map((v,i)=>{
                  return {
                    ...v,
                    value:v.id,
                    label:v.name,
                  }
              }))
              resolve(res)
              console.log("res",res.data)
          } else {
              commit('SET_DATA_FORM_OF_DRUG', {})
              resolve()
          }
      })
    })
  },
  getDict_dataKuFangList({ commit }, d){
    return new Promise((resolve, reject) => {
      _API.getDrug_storeIndex(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
              commit('SET_KUFANGLIST', res.data.map((v,i)=>{
                  return {
                    ...v,
                    value:v.id,
                    label:v.name,
                  }
              }))
              resolve(res)
              console.log("res",res.data)
          } else {
             
              commit('SET_KUFANGLIST', {})
              
              resolve()
          }
      })
    })
  },
  getSysbuildGetAllIcu({ commit }, d){
    return new Promise((resolve, reject) => {
      _API.getSysbuildGetAllIcu(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
              commit('SET_KUFANGLIST', res.data.map((v,i)=>{
                  return {
                    ...v,
                    value:v.id,
                    label:v.name,
                  }
              }))
              resolve(res)
              console.log("res",res.data)
          } else {
              commit('SET_KUFANGLIST', {})
              resolve()
          }
      })
    })
  },
   //药品分类
  getDrugCategoryIndex({ commit }, d){  
    return new Promise((resolve, reject) => {
      _API.getDrugCategoryIndex(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
              commit('SET_DATA_DRUGLIST', res.data.list.map((v,i)=>{
                  return {
                    ...v,
                    value:v.id,
                    label:v.name,
                  }
              }))
              resolve(res)
              console.log("res",res.data)
          } else {
              commit('SET_DATA_DRUGLIST', {})
              resolve()
          }
      })
    })
  },
    //药品种类
  getDrugDrugSpeciesIndex({ commit }, d){
    return new Promise((resolve, reject) => {
      _API.getDrugDrugSpeciesIndex(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
              commit('SET_DATA_SPECIES', res.data.map((v,i)=>{
                  return {
                    ...v,
                    value:v.id,
                    label:v.name,
                  }
              }))
              resolve(res)
              console.log("res",res.data)
          } else {
              commit('SET_DATA_SPECIES', {})
              resolve()
          }
      })
    })
  },
  getDrug_houseIndex({ commit }, d){
    return new Promise((resolve, reject) => {
      _API.getDrug_houseIndex(d).then((res)=>{
         
            if (res.code === 200 && res.data) {
              commit('SET_DATA_HOUSE', res.data.map((v,i)=>{
                  return {
                    ...v,
                    value:v.id,
                    label:v.name,
                  }
              }))
              resolve(res)
              console.log("res",res.data)
          } else {
              commit('SET_DATA_HOUSE', {})
              resolve()
          }
      })
    })
  },

 
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

